/*
 * Copyright (c) 2018. paascloud.net All Rights Reserved.
 * 项目名称：paascloud快速搭建企业级分布式微服务平台
 * 类名称：DateUtil.java
 * 创建人：刘兆明
 * 联系方式：paascloud.net@gmail.com
 * 开源地址: https://github.com/paascloud
 * 博客地址: http://blog.paascloud.net
 * 项目官网: http://paascloud.net
 */

package com.csun.cmny.util;


import com.xiaoleilu.hutool.date.DateField;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * The class Date util.
 *
 * @author paascloud.net @gmail.com
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DateUtil {

	/**
	 * 时间格式 yyyy-MM-dd HH:mm:ss
	 */
	public final static DateFormat YYYY_MM_DD_MM_HH_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 时间格式 yyyy-MM-dd'T'HH:mm:ss
	 */
	public final static DateFormat YYYY_MM_DD_T_MM_HH_SS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

	public final static DateFormat YYYYMMDDHHMMSS = new SimpleDateFormat("yyyyMMddHHmmss");

	/**
	 * 时间格式 yyyy-MM-dd
	 */
	public final static DateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");


	public static Date format_YYYY_MM_DD(Date date) throws ParseException {

		String str = YYYY_MM_DD.format(new Date());

		return YYYY_MM_DD.parse(str);
	}

	/**
	 * 将日期转为ISO 8601格式
	 *
	 * @param date	待转换的日期
	 *
	 * @return	转换后的日期
	 */
	public static String dateToIsoStr(Date date) {

		return YYYY_MM_DD_T_MM_HH_SS.format(date);
	}

	public static String dateToString(Date date){
		return YYYY_MM_DD_MM_HH_SS.format(date);
	}

	public static Date strToDate(String str) {

		Date date = new Date();

		try {
			date = YYYY_MM_DD_MM_HH_SS.parse(str);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return date;
	}

	/**
	 * 将日期转化成字符串格式，格式：yyyy-MM-dd
	 *
	 * @return
	 */
	public static String getDateStr(Date date) {
		return YYYY_MM_DD.format(date);
	}

	/**
	 * 获取系统前时间.
	 *
	 * @param minute the minute
	 *
	 * @return the before time[yyyy-MM-dd HH:mm:ss]
	 */
	public static String getBeforeTime(int minute) {
		Date newDate = com.xiaoleilu.hutool.date.DateUtil.offset(new Date(), DateField.MINUTE, -minute);
		return com.xiaoleilu.hutool.date.DateUtil.formatDateTime(newDate);
	}


	/**
	 * 计算两个时间之间相差的天数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static long diffDays(String startDate,String endDate){

		Long days = diffDays(stringToDate(startDate), stringToDate(endDate));

		return days;
	}

	public static Date stringToDate(String dateString){
		Date date = null;
		try {
			date = YYYY_MM_DD.parse(dateString);
		} catch (ParseException e) {
			//System.out.println("dateString = " + dateString);
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 计算两个时间之间相差的天数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static long diffDays(Date startDate,Date endDate){
		long days = 0;
		long start = startDate.getTime();
		long end = endDate.getTime();
		//一天的毫秒数1000 * 60 * 60 * 24=86400000
		days = (end - start) / 86400000;
		return days;
	}

	public static String dateToStr(Date date){
		return YYYY_MM_DD.format(date);
	}

	/**
	 * 日期加上天数的时间
	 * @param date
	 * @return
	 */
	public static Date dateAddDay(Date date,int day){
		return add(date, Calendar.DAY_OF_YEAR,day);
	}

	public static Date add(Date date,int type,int value){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(type, value);
		return calendar.getTime();
	}

	/**
	 * 获取指定日期是当月多少天
	 *
	 * @param date
	 * @return
	 */
	public static Integer getDaysByMonth(Date date) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public static int getDaysOfMonth(Date date) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}


	/**
	 * 获取yyyyMMddHHmmss的时间
	 */
	public static String nowDateStr() {

		YYYYMMDDHHMMSS.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));

		return YYYYMMDDHHMMSS.format(new Date());
	}

    public static String getLastMonth(String month) {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

		try {

			Date date = format.parse(month);

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date); // 设置为当前时间
			calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
			date = calendar.getTime();

			return format.format(date);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return month;
    }
}
